package www.hrabbit.cn.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import www.hrabbit.cn.mappper.FileMapper;
import www.hrabbit.cn.model.FTPProperties;
import www.hrabbit.cn.model.File;
import www.hrabbit.cn.service.FileService;
import www.hrabbit.cn.util.BaseToMultipart;
import www.hrabbit.cn.util.FTPUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * 业务层
 */
@Service("fileService")
public class FileServiceImpl implements FileService {

    /**
     * 注入Dao层
     */
    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private FTPProperties ftpProperties;

    /**
     * 获取配置文件
     * @return
     */
    public String getFTPProperties(String base64){
       try{
           MultipartFile multipartFile = BaseToMultipart.base64ToMultipart(base64);
           //设置生成的目录
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss");
           //生成目录
           String dir = sdf.format(new Date());
           //获取文件的名称
           String originalFilename = multipartFile.getOriginalFilename();
           //调用FTP上传工具
           boolean flag = FTPUtils.uploadFile(ftpProperties.getHostName(), ftpProperties.getUserName(), ftpProperties.getPassWord(), Integer.valueOf(ftpProperties.getPort()), dir, originalFilename, multipartFile.getInputStream());
           //如果上传成功
           String imageUrl="";
           if (flag){
               imageUrl = ftpProperties.getServerUrl()+"/"+dir+"/"+originalFilename;
           }
           return imageUrl;
       }catch (Exception e){
           e.printStackTrace();
       }
       return null;
    }

}
